package com.softa.web.controller.custom;

import com.github.pagehelper.PageInfo;
import com.softa.annotation.CurrentUser;
import com.softa.dao.dto.SysMessageDTO;
import com.softa.dao.dto.SysUserDTO;
import com.softa.dao.model.SysServer;
import com.softa.dao.model.SysUser;
import com.softa.service.system.CustomUserCenterService;
import com.softa.service.system.SysMessageService;
import com.softa.service.system.SysServerService;
import com.softa.service.system.UserService;
import com.softa.utils.Constants;
import com.softa.vo.DataGridRequest;
import com.softa.vo.DataGridResponse;
import com.softa.vo.PaginationResponse;
import com.softa.vo.QueryRequest;
import com.softa.web.controller.base.BaseController;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 个人中心
 * Created by yangl on 2017/8/11.
 */
@Controller
@RequestMapping("/custom/usercenter")
public class CustomUserCenterController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private CustomUserCenterService customUserCenterService;
    @Autowired
    private SysServerService sysServerService;
    @Autowired
    private SysMessageService sysMessageService;

    /**
     * 个人中心
     */
    @RequestMapping("page")
    public String page(@CurrentUser SysUser loginUser, Model model) {
        model.addAttribute("user", loginUser);
//        model = customUserCenterService.getPic(model, loginUser);
        return Constants.MENU_FRONT+"/user_center";
    }


    @RequestMapping(value = "/saveuserinfo", method = RequestMethod.POST)
    @ResponseBody
    public Integer update(SysUserDTO sysUserDTO) {
        logger.info("用户信息修改");
        Integer status = 2;//成功
        try {
            String message = userService.updateUser(sysUserDTO);
            if(message.equals("修改成功")){
                status = 2;
            }else if(message.equals("1")){
                status = 1;
            }
        } catch (Exception e) {
            status = 0;
            logger.error(e.getMessage());
        }
        return status;
    }

    @RequestMapping(value = "/checkpassword", method = RequestMethod.POST)
    @ResponseBody
    public Integer checkpassword(@CurrentUser SysUser loginUser, SysUser user) {
        Integer status = 1;//默认不正确
        try {
            String password = new SimpleHash(
                    "md5",
                    user.getPassword(),
                    ByteSource.Util.bytes(loginUser.getUsername()+loginUser.getSalt()),
                    1).toHex();
            if (loginUser.getPassword().equals(password)) {
                status = 0;
            } else {
                status = 1;
            }
        } catch (Exception e) {
            status = 1;
            logger.error(e.getMessage());
        }
        return status;
    }

    @RequestMapping(value = "/updatepassword", method = RequestMethod.POST)
    @ResponseBody
    public Integer update(@CurrentUser SysUser loginUser, SysUser user) {
        logger.info("用户修改密码");
        Integer status = 2;//成功
        loginUser.setPassword(user.getPassword());
        try {
            userService.changePassword(loginUser.getId(), user.getPassword());
            status = 2;
        } catch (Exception e) {
            status = 0;
            logger.error(e.getMessage());
        }
        return status;
    }

    @RequestMapping(value="/server",method = RequestMethod.GET)
    @ResponseBody
    public DataGridResponse listPage(DataGridRequest request, QueryRequest queryRequest) {
        PageInfo<SysServer> pageResult = sysServerService.queryUserByParam(request, queryRequest);
        return new DataGridResponse(pageResult);
    }

    @RequestMapping(value="/data/list",method = RequestMethod.POST)
    @ResponseBody
    public PaginationResponse messageList(DataGridRequest request, QueryRequest queryRequest) {
        PageInfo<SysMessageDTO> pageResult = sysMessageService.queryProductTypeByParam(request, queryRequest);
        return new PaginationResponse(pageResult, request.getRows());
    }

    @RequestMapping(value="/data/size",method = RequestMethod.GET)
    @ResponseBody
    public Integer messageSize(DataGridRequest request, QueryRequest queryRequest) {
        return sysMessageService.selectCountByCondition();
    }

    @RequestMapping(value="/messages/read",method = RequestMethod.POST)
    @ResponseBody
    public Integer messageRead(SysMessageDTO dto) {
        return sysMessageService.updateRead(dto.getIds());
    }

    @RequestMapping(value="/messages/delete",method = RequestMethod.POST)
    @ResponseBody
    public Integer messageDelete(SysMessageDTO dto) {
        return sysMessageService.deleteMessage(dto.getIds());
    }
}
